
/**
* @module im.message
* @author: Hjava
* @description: IM模块消息内包
* @since: 2018-07-22 14:47:15
*/
Object.defineProperty(exports, "__esModule", { value: true });
var im_constant_1 = __webpack_require__(/*! _im/im.constant */ "./src/core/im/im.constant.ts");
var bufferType_1 = __webpack_require__(/*! _util/bufferType */ "./src/core/utils/bufferType.ts");
var byteBuffer_1 = __webpack_require__(/*! _util/byteBuffer */ "./src/core/utils/byteBuffer.ts");
var constant_1 = __webpack_require__(/*! _util/constant */ "./src/core/utils/constant.ts");
var packet_1 = __webpack_require__(/*! _util/packet */ "./src/core/utils/packet.ts");
var protocol_1 = __webpack_require__(/*! _util/protocol */ "./src/core/utils/protocol.ts");
var userData_1 = __webpack_require__(/*! _util/userData */ "./src/core/utils/userData.ts");
var log_1 = __webpack_require__(/*! ./../utils/log */ "./src/core/utils/log.ts");
var IMMessage = {};
/**
 * 文本消息
 * @type {Function}
 */
IMMessage.IMTextMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_TEXT,
    uri: im_constant_1.default.IM.URI_IM_TEXT_INFO,
    init: function () {
        this.text = '';
        this.font_name = 'serif';
        this.font_size = 12;
        this.bold = false;
        this.cipher_type = constant_1.CIPHER_TYPE.NO_CIPHER;
    },
    marshall: function () {
        this.push(this.text, bufferType_1.default.STRING);
        this.push(this.font_name, bufferType_1.default.STRING);
        this.push(this.font_size, bufferType_1.default.INT);
        this.push(this.bold, bufferType_1.default.BOOLEAN);
    },
    unmarshall: function () {
        this.text = this.popString();
        this.font_name = this.popString();
        this.font_size = this.popInt();
        this.bold = this.popBool();
    }
});
/**
 * 图片消息
 * @type {Function}
 */
IMMessage.IMImageMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_IMAGE,
    uri: im_constant_1.default.IM.URI_IM_IMAGE_INFO,
    init: function () {
        this.thumbnail = '';
        this.normal = '';
        this.original = '';
        this.type = '';
        this.token = ''; //文件验证token
        this.originSize = 0;
        this.uploadImageType = 2;
        this.custom = '';
    },
    marshall: function () {
        this.push(this.thumbnail);
        this.push(this.normal);
        this.push(this.original);
        this.push(this.type);
        this.push(this.token);
        this.push(this.originSize);
        this.push(this.uploadImageType);
        this.push(this.custom);
    },
    unmarshall: function () {
        this.thumbnail = this.popString();
        this.normal = this.popString();
        this.original = this.popString();
        this.type = this.popString();
        this.token = this.popString();
        this.originSize = this.popInt();
        this.uploadImageType = this.popByte();
        this.custom = this.popString();
    }
});
/**
 * 单图文消息
 * @type {Function}
 */
IMMessage.IMLinkMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_LINK,
    uri: im_constant_1.default.IM.URI_IM_LINK_INFO,
    init: function () {
        this.title = '';
        this.image = '';
        this.content = '';
        this.link = '';
    },
    marshall: function () {
        this.push(this.title);
        this.push(this.image);
        this.push(this.content);
        this.push(this.link);
    },
    unmarshall: function () {
        this.title = this.popString();
        this.image = this.popString();
        this.content = this.popString();
        this.link = this.popString();
    }
});
/**
 * 多图文消息
 * @type {Function}
 */
IMMessage.IMMultiLinkMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_MULTI_LINK,
    uri: im_constant_1.default.IM.URI_IM_MULTI_LINK_INFO,
    init: function () {
        this.num = 0;
        this.content = '';
    },
    marshall: function () {
        this.push(this.num, bufferType_1.default.SHORT);
        this.push(this.content, bufferType_1.default.STRING);
    },
    unmarshall: function () {
        this.num = this.popShort();
        this.content = this.popString();
    }
});
/**
 * 表情消息
 * @type {Function}
 */
IMMessage.IMEmotionMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_EMOTION,
    uri: im_constant_1.default.IM.URI_IM_EMOTION_INFO,
    init: function () {
        this.category = '';
        this.type = '';
        this.name = '';
    },
    marshall: function () {
        this.push(this.category, bufferType_1.default.STRING);
        this.push(this.type, bufferType_1.default.STRING);
        this.push(this.name, bufferType_1.default.STRING);
    },
    unmarshall: function () {
        this.category = this.popString();
        this.type = this.popString();
        this.name = this.popString();
    }
});
/**
 * 事件消息
 * @type {Function}
 */
IMMessage.IMEventMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_EVENT,
    uri: im_constant_1.default.IM.URI_IM_EVENT_INFO,
    init: function () {
        this.type = '';
        this.text = '';
    },
    marshall: function () {
        this.push(this.type);
        this.push(this.text);
    },
    unmarshall: function () {
        this.type = this.popString();
        this.text = this.popString();
    }
});
/**
 * 日历消息
 * @type {Function}
 */
IMMessage.IMCalendarMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_CALENDAR,
    uri: im_constant_1.default.IM.URI_IM_CALENDAR_INFO,
    init: function () {
        this.dtstart = 0;
        this.dtend = 0;
        this.summary = '';
        this.location = '';
        this.trigger = '';
        this.participant = '';
        this.remark = '';
        this.calendarID = '';
    },
    marshall: function () {
        this.push(this.dtstart, bufferType_1.default.LONG);
        this.push(this.dtend, bufferType_1.default.LONG);
        this.push(this.summary);
        this.push(this.location);
        this.push(this.trigger);
        this.push(this.participant);
        this.push(this.remark);
        this.push(this.calendarID, bufferType_1.default.LONG);
    },
    unmarshall: function () {
        this.dtstart = this.popLong().value();
        this.dtend = this.popLong().value();
        this.summary = this.popString();
        this.location = this.popString();
        this.trigger = this.popString();
        this.participant = this.popString();
        this.remark = this.popString();
        this.calendarID = this.popLong().value();
    }
});
/**
 * 语音消息
 * @type {Function}
 */
IMMessage.IMAudioMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_VOICE,
    uri: im_constant_1.default.IM.URI_IM_AUDIO_INFO,
    init: function () {
        this.url = '';
        this.codec = 0;
        this.duration = 0; //语音时长
        this.stamp = 0; //语音发送时间
        this.token = ''; //文件验证token
        this.custom = '';
    },
    marshall: function () {
        this.push(this.url, bufferType_1.default.STRING);
        this.push(this.codec, bufferType_1.default.SHORT);
        this.push(this.duration, bufferType_1.default.SHORT);
        this.push(this.stamp, bufferType_1.default.LONG);
        this.push(this.token, bufferType_1.default.STRING);
        this.push(this.custom, bufferType_1.default.STRING);
    },
    unmarshall: function () {
        this.url = this.popString();
        this.codec = this.popShort();
        this.duration = this.popShort();
        this.stamp = this.popLong().value();
        this.token = this.popString();
        this.custom = this.popString();
    }
});
/**
 * 视频消息
 * @type {Function}
 */
IMMessage.IMVideoMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_VIDEO,
    uri: im_constant_1.default.IM.URI_IM_VIDEO_INFO,
    init: function () {
        this.videoUrl = '';
        this.screenshotUrl = '';
        this.duration = 0;
        this.size = 0;
        this.width = 0;
        this.height = 0;
        this.timestamp = 0;
        this.token = ''; //文件验证token
        this.custom = '';
    },
    marshall: function () {
        this.push(this.videoUrl, bufferType_1.default.STRING);
        this.push(this.screenshotUrl, bufferType_1.default.STRING);
        this.push(this.duration, bufferType_1.default.INT);
        this.push(this.size, bufferType_1.default.INT);
        this.push(this.width, bufferType_1.default.SHORT);
        this.push(this.height, bufferType_1.default.SHORT);
        this.push(this.timestamp, bufferType_1.default.LONG);
        this.push(this.token, bufferType_1.default.STRING);
        this.push(this.custom, bufferType_1.default.STRING);
    },
    unmarshall: function () {
        this.videoUrl = this.popString();
        this.screenshotUrl = this.popString();
        this.duration = this.popInt();
        this.size = this.popInt();
        this.width = this.popShort();
        this.height = this.popShort();
        this.timestamp = this.popLong().value();
        this.token = this.popString();
        this.custom = this.popString();
    }
});
/**
 * 位置消息
 * @type {Function}
 */
IMMessage.IMGPSMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_GPS,
    uri: im_constant_1.default.IM.URI_IM_GPS_INFO,
    init: function () {
        this.latitude = 0;
        this.longitude = 0;
        this.name = '';
    },
    marshall: function () {
        this.push(this.latitude * 1000000);
        this.push(this.longitude * 1000000);
        this.push(this.name);
    },
    unmarshall: function () {
        this.latitude = this.popInt() / 1000000; //转换为实际数值
        this.longitude = this.popInt() / 1000000;
        this.name = this.popString();
    }
});
/**
 * VCard消息
 * @type {Function}
 */
IMMessage.IMVCardMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_VCARD,
    uri: im_constant_1.default.IM.URI_IM_VCARD_INFO,
    init: function () {
        this.uri = im_constant_1.default.IM.URI_IM_VCARD_INFO;
        this.uid = 0;
        this.name = '';
        this.account = '';
        this.type = 0;
        this.subtype = 0;
    },
    marshall: function () {
        this.push(this.uid, bufferType_1.default.LONG);
        this.push(this.name);
        this.push(this.account);
        this.push(this.type, bufferType_1.default.SHORT);
        this.push(this.subtype, bufferType_1.default.SHORT);
    },
    unmarshall: function () {
        this.uid = this.popLong().value();
        this.name = this.popString();
        this.account = this.popString();
        this.type = this.popShort();
        this.subtype = this.popShort();
    }
});
/**
 * 文件消息
 * @type {Function}
 */
IMMessage.IMFileMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_FILE,
    uri: im_constant_1.default.IM.URI_IM_FILE_INFO,
    init: function () {
        this.id = '';
        this.url = '';
        this.name = '';
        this.format = '';
        this.size = 0;
        this.token = ''; //文件验证token
        this.lsize = 0;
        this.custom = '';
    },
    marshall: function () {
        this.push(this.id);
        this.push(this.url);
        this.push(this.name);
        this.push(this.format);
        this.push(this.size);
        this.push(this.token);
        this.push(this.lsize, bufferType_1.default.LONG);
        this.push(this.custom);
    },
    unmarshall: function () {
        this.id = this.popString();
        this.url = this.popString();
        this.name = this.popString();
        this.format = this.popString();
        this.size = this.popInt();
        this.token = this.popString();
        this.lsize = this.popLong().value();
        this.custom = this.popString();
    }
});
/**
 * 自定义消息
 * @type {Function}
 */
IMMessage.IMCustomInfo = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_CUSTOM,
    uri: im_constant_1.default.IM.URI_IM_CUSTOM_INFO,
    init: function () {
        this.templateName = '';
        this.contentTitle = '';
        this.content = '';
        this.linkName = '';
        this.link = 0;
    },
    marshall: function () {
        this.push(this.templateName);
        this.push(this.contentTitle);
        this.push(this.content);
        this.push(this.linkName);
        this.push(this.link);
    },
    unmarshall: function () {
        this.templateName = this.popString();
        this.contentTitle = this.popString();
        this.content = this.popString();
        this.linkName = this.popString();
        this.link = this.popString();
    }
});
/**
 * 红包消息
 * @type {{Function}}
 */
IMMessage.IMRedPacketInfo = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_RED_PACKET,
    uri: im_constant_1.default.IM.URI_IM_REDPACKET_INFO,
    init: function () {
        this.rpid = 0;
        this.type = 0; // 1:拼手气红包 2:普通红包 3:单个红包
        this.greetings = '';
    },
    marshall: function () {
        this.push(this.rpid, bufferType_1.default.LONG);
        this.push(this.type, bufferType_1.default.SHORT);
        this.push(this.greetings, bufferType_1.default.STRING);
    },
    unmarshall: function () {
        this.rpid = this.popLong().value();
        this.type = this.popShort();
        this.greetings = this.popString();
    }
});
/**
 * 群名片消息
 */
IMMessage.IMGVCardInfo = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_GVCARD,
    uri: im_constant_1.default.IM.URI_IM_GVCARD_INFO,
    init: function () {
        this.guid = 0;
        this.name = '';
        this.account = '';
        this.type = 0;
        this.subtype = 0;
    },
    marshall: function () {
        this.push(this.guid, bufferType_1.default.LONG);
        this.push(this.name, bufferType_1.default.STRING);
        this.push(this.account, bufferType_1.default.STRING);
        this.push(this.type, bufferType_1.default.SHORT);
        this.push(this.subtype, bufferType_1.default.SHORT);
    },
    unmarshall: function () {
        this.guid = this.popLong().value();
        this.name = this.popString();
        this.account = this.popString();
        this.type = this.popShort();
        this.subtype = this.popShort();
    }
});
IMMessage.IMPCallMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_PCALL,
    uri: im_constant_1.default.IM.URI_IM_PCALL_INFO,
    init: function () {
        this.uid = 0;
        this.peerUid = '';
        this.roles = '';
        this.callStatus = 0;
        this.callType = 0;
        this.startCallTs = 0;
        this.startTalkTs = 0;
        this.endTs = 0;
        this.callDur = 0;
    },
    marshall: function () {
        this.push(this.uid, bufferType_1.default.LONG);
        this.push(this.peerUid, bufferType_1.default.LONG);
        this.push(this.roles, bufferType_1.default.INT);
        this.push(this.callStatus, bufferType_1.default.INT);
        this.push(this.callType, bufferType_1.default.INT);
        this.push(this.startCallTs, bufferType_1.default.LONG);
        this.push(this.startTalkTs, bufferType_1.default.LONG);
        this.push(this.endTs, bufferType_1.default.LONG);
        this.push(this.callDur, bufferType_1.default.LONG);
    },
    unmarshall: function () {
        this.uid = this.popLong().value();
        this.peerUid = this.popLong().value();
        this.roles = this.popInt();
        this.callStatus = this.popInt();
        this.callType = this.popInt();
        this.startCallTs = this.popLong().value();
        this.startTalkTs = this.popLong().value();
        this.endTs = this.popLong().value();
        this.callDur = this.popLong().value();
    }
});
IMMessage.IMGeneralMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_GENERAL,
    uri: im_constant_1.default.IM.URL_IM_GENERAL_INFO,
    init: function () {
        this.data = {};
        this.type = 0;
        this.summary = '';
    },
    marshall: function () {
        this.push(byteBuffer_1.default.strToBytes(this.data), bufferType_1.default.BYTES);
        this.push(this.type, bufferType_1.default.INT);
        this.push(this.summary, bufferType_1.default.STRING);
    },
    unmarshall: function () {
        var buffer = this.popBytes();
        var data = {};
        var innerDataBuffer;
        var appId = userData_1.default.get('appId');
        this.type = this.popInt();
        if (appId === constant_1.APPID.DX) {
            // 为大象做特殊处理
            switch (this.type) {
                case constant_1.GENERAL_TYPE.TYPE_CALL_1V1: {
                    try {
                        // 没有uri的内包，先放在第二层包中进行解析，结构见：http://git.sankuai.com/projects/XM/repos/xm-common/browse/xm-protocol/src/main/java/com/sankuai/xm/proto/call/msg/PCallMsg1v1.java
                        innerDataBuffer = new packet_1.default();
                        innerDataBuffer.buffer = new byteBuffer_1.default(buffer);
                        // 协议头不需要，直接舍弃
                        innerDataBuffer.popInt();
                        innerDataBuffer.popInt();
                        innerDataBuffer.popShort();
                        data.creator = innerDataBuffer.popUItem();
                        data.invitee = innerDataBuffer.popUItem();
                        data.startTs = innerDataBuffer.popLong().value();
                        data.endTs = innerDataBuffer.popLong().value();
                        data.sid = innerDataBuffer.popString();
                        data.callType = innerDataBuffer.popByte();
                        data.callStatus = innerDataBuffer.popByte();
                        this.data = data;
                    }
                    catch (error) {
                        log_1.default.error('IMGeneralMessage::unmarshall error appid=dx type:' + this.type);
                        this.data = (new byteBuffer_1.default(buffer)).readUTF8String(buffer.byteLength);
                    }
                    break;
                }
                case constant_1.GENERAL_TYPE.TYPE_CALL_MEETING: {
                    try {
                        // 没有uri的内包，先放在第二层包中进行解析，结构见：http://git.sankuai.com/projects/XM/repos/xm-common/browse/xm-protocol/src/main/java/com/sankuai/xm/proto/call/msg/PCallMsgMeeting.java
                        innerDataBuffer = new packet_1.default();
                        innerDataBuffer.buffer = new byteBuffer_1.default(buffer);
                        // 协议头不需要，直接舍弃
                        innerDataBuffer.popInt();
                        innerDataBuffer.popInt();
                        innerDataBuffer.popShort();
                        data.creator = innerDataBuffer.popUItem();
                        data.startTs = innerDataBuffer.popLong().value();
                        data.endTs = innerDataBuffer.popLong().value();
                        data.gid = innerDataBuffer.popLong().value();
                        data.sid = innerDataBuffer.popString();
                        data.text = innerDataBuffer.popString();
                        this.data = data;
                    }
                    catch (error) {
                        log_1.default.error('IMGeneralMessage::unmarshall error appid=dx type:' + this.type);
                        this.data = (new byteBuffer_1.default(buffer)).readUTF8String(buffer.byteLength);
                    }
                    break;
                }
                case constant_1.GENERAL_TYPE.TYPE_GROUP_VOTE: {
                    this.data = (new byteBuffer_1.default(buffer)).readUTF8String(buffer.byteLength);
                    break;
                }
                default: {
                    this.data = (new byteBuffer_1.default(buffer)).readUTF8String(buffer.byteLength);
                }
            }
        }
        else {
            this.data = (new byteBuffer_1.default(buffer)).readUTF8String(buffer.byteLength);
        }
        try {
            this.summary = this.popString();
        }
        catch (error) {
            log_1.default.error('IMGeneralMessage::unmarshall error summary:' + error);
            this.summary = '';
        }
    }
});
IMMessage.IMNewEmotionMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_NEWEMOTION,
    uri: im_constant_1.default.IM.URI_IM_NEWEMOTION_INFO,
    init: function () {
        this.category = '';
        this.packageId = '';
        this.packageName = '';
        this.id = '';
        this.name = '';
        this.type = '';
        this.params = '';
    },
    marshall: function () {
        this.push(this.category, bufferType_1.default.STRING);
        this.push(this.packageId, bufferType_1.default.STRING);
        this.push(this.packageName, bufferType_1.default.STRING);
        this.push(this.id, bufferType_1.default.STRING);
        this.push(this.name, bufferType_1.default.STRING);
        this.push(this.type, bufferType_1.default.STRING);
        this.push(this.params, bufferType_1.default.STRING);
    },
    unmarshall: function () {
        this.category = this.popString();
        this.packageId = this.popString();
        this.packageName = this.popString();
        this.id = this.popString();
        this.name = this.popString();
        this.type = this.popString();
        this.params = this.popString();
    }
});
/**
 * 引用消息
 */
IMMessage.IMQuoteMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_QUOTE,
    uri: im_constant_1.default.IM.URI_IM_QUOTE_INFO,
    init: function () {
        this.lastReply = '{}';
        this.quoted = '[]';
        this.searchText = '';
    },
    marshall: function () {
        this.push(this.lastReply, bufferType_1.default.STRING);
        this.push(this.quoted, bufferType_1.default.STRING);
        this.push(this.searchText, bufferType_1.default.STRING);
    },
    unmarshall: function () {
        try {
            this.lastReply = this.popString();
            this.quoted = this.popString();
            this.searchText = this.popString();
            var lastReplyMessage = JSON.parse(this.lastReply);
            lastReplyMessage = {
                message: lastReplyMessage.message ? protocol_1.default.Response.fromBase64(lastReplyMessage.message) : null,
                defaultText: lastReplyMessage.defaultText
            };
            this.lastReplyMessage = lastReplyMessage;
            var quotedObject = null;
            try {
                quotedObject = JSON.parse(this.quoted);
            }
            catch (e) {
                log_1.default.error('IMQuoteMessage::unmarshall JSON.parse error info:' + e);
            }
            if (quotedObject) {
                this.quotedMessage = quotedObject.map(function (item) {
                    return {
                        message: protocol_1.default.Response.fromBase64(item.message),
                        defaultText: item.defaultText
                    };
                });
            }
        }
        catch (e) {
            log_1.default.error('IMQuoteMessage::unmarshall error info:' + e);
        }
    }
});
IMMessage.IMDynamicMessage = protocol_1.default.Message.extend({
    __category: 'IM',
    __type: constant_1.MESSAGE_TYPE.MSG_TYPE_DYNAMIC,
    uri: im_constant_1.default.IM.URI_IM_DYNAMIC_INFO,
    init: function () {
        this.id = '';
        this.title = '';
        this.dxData = '';
        this.appData = '';
    },
    marshall: function () {
        this.push(this.id, bufferType_1.default.LONG);
        this.push(this.title, bufferType_1.default.STRING);
        this.push(this.dxData, bufferType_1.default.STRING);
        this.push(this.appData, bufferType_1.default.STRING);
    },
    unmarshall: function () {
        this.id = this.popLong().value();
        this.title = this.popString();
        this.dxData = this.popString();
        this.appData = this.popString();
    }
});
exports.default = IMMessage;


//# sourceURL=webpack://MTDX/./src/core/im/im.message.ts?